springboot+mybatis通过实体类自动生成数据库表的方法 |
您所在的位置:网站首页 › mybatis-enhance-actable db2 › springboot+mybatis通过实体类自动生成数据库表的方法 |
springboot+mybatis 通过实体类⾃动⽣成数据库表的⽅法 前⾔ 本章介绍使⽤ mybatis 结合 mysql 数据库⾃动根据实体类⽣成相关的数据库表。 ⾸先引⼊相关的 pom 包我这⾥使⽤的是 springboot2.1.8.RELEASE 的版本
org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.0
com.gitee.sunchenbin.mybatis.actable mybatis-enhance-actable 1.0.1
mysql mysql-connector-java runtime
com.alibaba druid-spring-boot-starter 1.1.10
org.apache.commons commons-lang3 3.4
net.sf.json-lib json-lib 2.4 jdk15
commons-logging commons-logging
添加数据库配置⽂件 application.properties application.properties 这⾥是单独配置 mybatis ⾃动建表的相关信息。 mybatis.table.auto=update mybatis.model.pack=com.xxx.xxx.entity// 实体类的路径 mybatis.database.type=mysql mybatis.table.auto= create : 每次加载 hibernate 会⾃动创建表,以后启动会覆盖之前的表,所以这个值基本不⽤,严重会导致的数据的丢失。 create-drop : 每次加载 hibernate 时根据 model 类⽣成表,但是 sessionFactory ⼀关闭,表就⾃动删除,下⼀次启动会重新创建。 update : 加载 hibernate 时根据实体类 model 创建数据库表,这是表名的依据是 @Entity 注解的值或者 @Table 注解的值, sessionFactory 关闭表不会删除,且下⼀次启动会根据实体。 model : 更新结构或者有新的实体类会创建新的表。 validate : 启动时验证表的结构,不会创建表 none :启动时不做任何操作 mybatis.model.pack=com.xxx.xxx.entity// 你实体类的路径 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |